
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3-11 构建同构应用 · 深入浅出 Webpack</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="吴浩麟">
        <meta name="identifier" content="9787121331725" scheme="ISBN">
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-modal/index.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        

    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3-12构建Electron应用.html" />
    
    
    <link rel="prev" href="3-10管理多个单页应用.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"gwuhaolin","type":"follow","size":"small","width":"150"},{"user":"gwuhaolin","repo":"dive-into-webpack","type":"star","size":"small"}]};
    </script>


    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
  
  

  

  
  
  

<li class="chapter " data-level="1.1" data-path="../">
  
  <a href="../">
    
                    
                    深入浅出 Webpack
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.2" data-path="../前言.html">
  
  <a href="../前言.html">
    
                    
                    前言
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3" data-path="../1入门/">
  
  <a href="../1入门/">
    
                    
                    第1章 入门
            
    </a>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.3.1" data-path="../1入门/1-1前端的发展.html">
  
  <a href="../1入门/1-1前端的发展.html">
    
                    
                    1-1 前端的发展
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.2" data-path="../1入门/1-2常见的构建工具及对比.html">
  
  <a href="../1入门/1-2常见的构建工具及对比.html">
    
                    
                    1-2 常见的构建工具及对比
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.3" data-path="../1入门/1-3安装与使用.html">
  
  <a href="../1入门/1-3安装与使用.html">
    
                    
                    1-3 安装与使用
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.4" data-path="../1入门/1-4使用Loader.html">
  
  <a href="../1入门/1-4使用Loader.html">
    
                    
                    1-4 使用 Loader
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.5" data-path="../1入门/1-5使用Plugin.html">
  
  <a href="../1入门/1-5使用Plugin.html">
    
                    
                    1-5 使用 Plugin
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.6" data-path="../1入门/1-6使用DevServer.html">
  
  <a href="../1入门/1-6使用DevServer.html">
    
                    
                    1-6 使用 DevServer
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.3.7" data-path="../1入门/1-7核心概念.html">
  
  <a href="../1入门/1-7核心概念.html">
    
                    
                    1-7 核心概念
            
    </a>
    

    
</li>


    </ul>
    
</li>

<li class="chapter " data-level="1.4" data-path="../2配置/">
  
  <a href="../2配置/">
    
                    
                    第2章 配置
            
    </a>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.4.1" data-path="../2配置/2-1Entry.html">
  
  <a href="../2配置/2-1Entry.html">
    
                    
                    2-1 Entry
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.2" data-path="../2配置/2-2Output.html">
  
  <a href="../2配置/2-2Output.html">
    
                    
                    2-2 Output
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.3" data-path="../2配置/2-3Module.html">
  
  <a href="../2配置/2-3Module.html">
    
                    
                    2-3 Module
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.4" data-path="../2配置/2-4Resolve.html">
  
  <a href="../2配置/2-4Resolve.html">
    
                    
                    2-4 Resolve
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.5" data-path="../2配置/2-5Plugins.html">
  
  <a href="../2配置/2-5Plugins.html">
    
                    
                    2-5 Plugins
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.6" data-path="../2配置/2-6DevServer.html">
  
  <a href="../2配置/2-6DevServer.html">
    
                    
                    2-6 DevServer
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.7" data-path="../2配置/2-7其它配置项.html">
  
  <a href="../2配置/2-7其它配置项.html">
    
                    
                    2-7 其它配置项
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.8" data-path="../2配置/2-8整体配置结构.html">
  
  <a href="../2配置/2-8整体配置结构.html">
    
                    
                    2-8 整体配置结构
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.9" data-path="../2配置/2-9多种配置类型.html">
  
  <a href="../2配置/2-9多种配置类型.html">
    
                    
                    2-9 多种配置类型
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.4.10" data-path="../2配置/2-10配置总结.html">
  
  <a href="../2配置/2-10配置总结.html">
    
                    
                    2-10 配置总结
            
    </a>
    

    
</li>


    </ul>
    
</li>

<li class="chapter " data-level="1.5" data-path="./">
  
  <a href="./">
    
                    
                    第3章 实战
            
    </a>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.5.1" data-path="3-1使用ES6语言.html">
  
  <a href="3-1使用ES6语言.html">
    
                    
                    3-1 使用 ES6 语言
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.2" data-path="3-2使用TypeScript语言.html">
  
  <a href="3-2使用TypeScript语言.html">
    
                    
                    3-2 使用 TypeScript 语言
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.3" data-path="3-3使用Flow检查器.html">
  
  <a href="3-3使用Flow检查器.html">
    
                    
                    3-3 使用 Flow 检查器
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.4" data-path="3-4使用SCSS语言.html">
  
  <a href="3-4使用SCSS语言.html">
    
                    
                    3-4 使用 SCSS
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.5" data-path="3-5使用PostCSS.html">
  
  <a href="3-5使用PostCSS.html">
    
                    
                    3-5 使用 PostCSS
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.6" data-path="3-6使用React框架.html">
  
  <a href="3-6使用React框架.html">
    
                    
                    3-6 使用 React 框架
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.7" data-path="3-7使用Vue框架.html">
  
  <a href="3-7使用Vue框架.html">
    
                    
                    3-7 使用 Vue 框架
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.8" data-path="3-8使用Angular2框架.html">
  
  <a href="3-8使用Angular2框架.html">
    
                    
                    3-8 使用 Angular2 框架
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.9" data-path="3-9为单页应用生成HTML.html">
  
  <a href="3-9为单页应用生成HTML.html">
    
                    
                    3-9 为单页应用生成 HTML
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.10" data-path="3-10管理多个单页应用.html">
  
  <a href="3-10管理多个单页应用.html">
    
                    
                    3-10 管理多个单页应用
            
    </a>
    

    
</li>

<li class="chapter active" data-level="1.5.11" data-path="3-11构建同构应用.html">
  
  <a href="3-11构建同构应用.html">
    
                    
                    3-11 构建同构应用
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.12" data-path="3-12构建Electron应用.html">
  
  <a href="3-12构建Electron应用.html">
    
                    
                    3-12 构建 Electron 应用
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.13" data-path="3-13构建Npm模块.html">
  
  <a href="3-13构建Npm模块.html">
    
                    
                    3-13 构建 Npm 模块
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.14" data-path="3-14构建离线应用.html">
  
  <a href="3-14构建离线应用.html">
    
                    
                    3-14 构建离线应用
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.15" data-path="3-15搭配NpmScript.html">
  
  <a href="3-15搭配NpmScript.html">
    
                    
                    3-15 搭配 Npm Script
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.16" data-path="3-16检查代码.html">
  
  <a href="3-16检查代码.html">
    
                    
                    3-16 检查代码
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.17" data-path="3-17通过Node.jsAPI启动Webpack.html">
  
  <a href="3-17通过Node.jsAPI启动Webpack.html">
    
                    
                    3-17 通过 Node.js API 启动 Webpack
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.18" data-path="3-18使用WebpackDevMiddleware.html">
  
  <a href="3-18使用WebpackDevMiddleware.html">
    
                    
                    3-18 使用 Webpack Dev Middleware
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.19" data-path="3-19加载图片.html">
  
  <a href="3-19加载图片.html">
    
                    
                    3-19 加载图片
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.20" data-path="3-20加载SVG.html">
  
  <a href="3-20加载SVG.html">
    
                    
                    3-20 加载SVG
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.21" data-path="3-21加载SourceMap.html">
  
  <a href="3-21加载SourceMap.html">
    
                    
                    3-21 加载 Source Map
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.5.22" data-path="3-22实战总结.html">
  
  <a href="3-22实战总结.html">
    
                    
                    3-22 实战总结
            
    </a>
    

    
</li>


    </ul>
    
</li>

<li class="chapter " data-level="1.6" data-path="../4优化/">
  
  <a href="../4优化/">
    
                    
                    第4章 优化
            
    </a>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.6.1" data-path="../4优化/4-1缩小文件搜索范围.html">
  
  <a href="../4优化/4-1缩小文件搜索范围.html">
    
                    
                    4-1 缩小文件搜索范围
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.2" data-path="../4优化/4-2使用DllPlugin.html">
  
  <a href="../4优化/4-2使用DllPlugin.html">
    
                    
                    4-2 使用 DllPlugin
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.3" data-path="../4优化/4-3使用HappyPack.html">
  
  <a href="../4优化/4-3使用HappyPack.html">
    
                    
                    4-3 使用 HappyPack
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.4" data-path="../4优化/4-4使用ParallelUglifyPlugin.html">
  
  <a href="../4优化/4-4使用ParallelUglifyPlugin.html">
    
                    
                    4-4 使用 ParallelUglifyPlugin
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.5" data-path="../4优化/4-5使用自动刷新.html">
  
  <a href="../4优化/4-5使用自动刷新.html">
    
                    
                    4-5 使用自动刷新
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.6" data-path="../4优化/4-6开启模块热替换.html">
  
  <a href="../4优化/4-6开启模块热替换.html">
    
                    
                    4-6 开启模块热替换
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.7" data-path="../4优化/4-7区分环境.html">
  
  <a href="../4优化/4-7区分环境.html">
    
                    
                    4-7 区分环境
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.8" data-path="../4优化/4-8压缩代码.html">
  
  <a href="../4优化/4-8压缩代码.html">
    
                    
                    4-8 压缩代码
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.9" data-path="../4优化/4-9CDN加速.html">
  
  <a href="../4优化/4-9CDN加速.html">
    
                    
                    4-9 CDN 加速
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.10" data-path="../4优化/4-10使用TreeShaking.html">
  
  <a href="../4优化/4-10使用TreeShaking.html">
    
                    
                    4-10 使用 Tree Shaking
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.11" data-path="../4优化/4-11提取公共代码.html">
  
  <a href="../4优化/4-11提取公共代码.html">
    
                    
                    4-11 提取公共代码
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.12" data-path="../4优化/4-12按需加载.html">
  
  <a href="../4优化/4-12按需加载.html">
    
                    
                    4-12 按需加载
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.13" data-path="../4优化/4-13使用Prepack.html">
  
  <a href="../4优化/4-13使用Prepack.html">
    
                    
                    4-13 使用 Prepack
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.14" data-path="../4优化/4-14开启ScopeHoisting.html">
  
  <a href="../4优化/4-14开启ScopeHoisting.html">
    
                    
                    4-14 开启 Scope Hoisting
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.15" data-path="../4优化/4-15输出分析.html">
  
  <a href="../4优化/4-15输出分析.html">
    
                    
                    4-15 输出分析
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.6.16" data-path="../4优化/4-16优化总结.html">
  
  <a href="../4优化/4-16优化总结.html">
    
                    
                    4-16 优化总结
            
    </a>
    

    
</li>


    </ul>
    
</li>

<li class="chapter " data-level="1.7" data-path="../5原理/">
  
  <a href="../5原理/">
    
                    
                    第5章 原理
            
    </a>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.7.1" data-path="../5原理/5-1工作原理概括.html">
  
  <a href="../5原理/5-1工作原理概括.html">
    
                    
                    5-1 工作原理概括
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.7.2" data-path="../5原理/5-2输出文件分析.html">
  
  <a href="../5原理/5-2输出文件分析.html">
    
                    
                    5-2 输出文件分析
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.7.3" data-path="../5原理/5-3编写Loader.html">
  
  <a href="../5原理/5-3编写Loader.html">
    
                    
                    5-3 编写 Loader
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.7.4" data-path="../5原理/5-4编写Plugin.html">
  
  <a href="../5原理/5-4编写Plugin.html">
    
                    
                    5-4 编写 Plugin
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.7.5" data-path="../5原理/5-5调试Webpack.html">
  
  <a href="../5原理/5-5调试Webpack.html">
    
                    
                    5-5 调试 Webpack
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.7.6" data-path="../5原理/5-6原理总结.html">
  
  <a href="../5原理/5-6原理总结.html">
    
                    
                    5-6 原理总结
            
    </a>
    

    
</li>


    </ul>
    
</li>

<li class="chapter " data-level="1.8" >
  
      <span>
            
                    
                    附录
            
    </span>
    

    
    <ul class="articles">
      

<li class="chapter " data-level="1.8.1" data-path="../附录/常用Loaders.html">
  
  <a href="../附录/常用Loaders.html">
    
                    
                    常用 Loaders
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.8.2" data-path="../附录/常用Plugins.html">
  
  <a href="../附录/常用Plugins.html">
    
                    
                    常用 Plugins
            
    </a>
    

    
</li>

<li class="chapter " data-level="1.8.3" data-path="../附录/其它Webpack学习资源.html">
  
  <a href="../附录/其它Webpack学习资源.html">
    
                    
                    其它 Webpack 学习资源
            
    </a>
    

    
</li>


    </ul>
    
</li>


  

  <li class="divider"></li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3-11 构建同构应用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="3-11-&#x6784;&#x5EFA;&#x540C;&#x6784;&#x5E94;&#x7528;">3-11 &#x6784;&#x5EFA;&#x540C;&#x6784;&#x5E94;&#x7528;</h1>
<p>&#x540C;&#x6784;&#x5E94;&#x7528;&#x662F;&#x6307;&#x5199;&#x4E00;&#x4EFD;&#x4EE3;&#x7801;&#x4F46;&#x53EF;&#x540C;&#x65F6;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x4E2D;&#x8FD0;&#x884C;&#x7684;&#x5E94;&#x7528;&#x3002;</p>
<h2 id="&#x8BA4;&#x8BC6;&#x540C;&#x6784;&#x5E94;&#x7528;">&#x8BA4;&#x8BC6;&#x540C;&#x6784;&#x5E94;&#x7528;</h2>
<p>&#x73B0;&#x5728;&#x5927;&#x591A;&#x6570;&#x5355;&#x9875;&#x5E94;&#x7528;&#x7684;&#x89C6;&#x56FE;&#x90FD;&#x662F;&#x901A;&#x8FC7; JavaScript &#x4EE3;&#x7801;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6E32;&#x67D3;&#x51FA;&#x6765;&#x7684;&#xFF0C;&#x4F46;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x574F;&#x5904;&#x6709;&#xFF1A;</p>
<ul>
<li>&#x641C;&#x7D22;&#x5F15;&#x64CE;&#x65E0;&#x6CD5;&#x6536;&#x5F55;&#x4F60;&#x7684;&#x7F51;&#x9875;&#xFF0C;&#x56E0;&#x4E3A;&#x5C55;&#x793A;&#x51FA;&#x7684;&#x6570;&#x636E;&#x90FD;&#x662F;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x5F02;&#x6B65;&#x6E32;&#x67D3;&#x51FA;&#x6765;&#x7684;&#xFF0C;&#x5927;&#x90E8;&#x5206;&#x722C;&#x866B;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x5230;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x590D;&#x6742;&#x7684;&#x5355;&#x9875;&#x5E94;&#x7528;&#xFF0C;&#x6E32;&#x67D3;&#x8FC7;&#x7A0B;&#x8BA1;&#x7B97;&#x91CF;&#x5927;&#xFF0C;&#x5BF9;&#x4F4E;&#x7AEF;&#x79FB;&#x52A8;&#x8BBE;&#x5907;&#x6765;&#x8BF4;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x6027;&#x80FD;&#x95EE;&#x9898;&#xFF0C;&#x7528;&#x6237;&#x80FD;&#x660E;&#x663E;&#x611F;&#x77E5;&#x5230;&#x9996;&#x5C4F;&#x7684;&#x6E32;&#x67D3;&#x5EF6;&#x8FDF;&#x3002;</li>
</ul>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x4EE5;&#x4E0A;&#x95EE;&#x9898;&#xFF0C;&#x6709;&#x4EBA;&#x63D0;&#x51FA;&#x80FD;&#x5426;&#x5C06;&#x539F;&#x672C;&#x53EA;&#x8FD0;&#x884C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x7684; JavaScript &#x6E32;&#x67D3;&#x4EE3;&#x7801;&#x4E5F;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8FD0;&#x884C;&#xFF0C;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;&#x51FA;&#x5E26;&#x5185;&#x5BB9;&#x7684; HTML &#x540E;&#x518D;&#x8FD4;&#x56DE;&#x3002;
&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x8BA9;&#x641C;&#x7D22;&#x5F15;&#x64CE;&#x722C;&#x866B;&#x76F4;&#x63A5;&#x6293;&#x53D6;&#x5230;&#x5E26;&#x6570;&#x636E;&#x7684; HTML&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x80FD;&#x964D;&#x4F4E;&#x9996;&#x5C4F;&#x6E32;&#x67D3;&#x65F6;&#x95F4;&#x3002;
&#x7531;&#x4E8E; Node.js &#x7684;&#x6D41;&#x884C;&#x548C;&#x6210;&#x719F;&#xFF0C;&#x4EE5;&#x53CA;&#x865A;&#x62DF; DOM &#x63D0;&#x51FA;&#x4E0E;&#x5B9E;&#x73B0;&#xFF0C;&#x4F7F;&#x8FD9;&#x4E2A;&#x5047;&#x8BBE;&#x6210;&#x4E3A;&#x53EF;&#x80FD;&#x3002;</p>
<p>&#x5B9E;&#x9645;&#x4E0A;&#x73B0;&#x5728;&#x4E3B;&#x6D41;&#x7684;&#x524D;&#x7AEF;&#x6846;&#x67B6;&#x90FD;&#x652F;&#x6301;&#x540C;&#x6784;&#xFF0C;&#x5305;&#x62EC; React&#x3001;Vue2&#x3001;Angular2&#xFF0C;&#x5176;&#x4E2D;&#x6700;&#x5148;&#x652F;&#x6301;&#x4E5F;&#x662F;&#x6700;&#x6210;&#x719F;&#x7684;&#x540C;&#x6784;&#x65B9;&#x6848;&#x662F; React&#x3002;
&#x7531;&#x4E8E; React &#x4F7F;&#x7528;&#x8005;&#x66F4;&#x591A;&#xFF0C;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x53C8;&#x5F88;&#x76F8;&#x4F3C;&#xFF0C;&#x672C;&#x8282;&#x53EA;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x7528; Webpack &#x6784;&#x5EFA; React &#x540C;&#x6784;&#x5E94;&#x7528;&#x3002;</p>
<p>&#x540C;&#x6784;&#x5E94;&#x7528;&#x8FD0;&#x884C;&#x539F;&#x7406;&#x7684;&#x6838;&#x5FC3;&#x5728;&#x4E8E;&#x865A;&#x62DF; DOM&#xFF0C;&#x865A;&#x62DF; DOM &#x7684;&#x610F;&#x601D;&#x662F;&#x4E0D;&#x76F4;&#x63A5;&#x64CD;&#x4F5C; DOM &#x800C;&#x662F;&#x901A;&#x8FC7; JavaScript Object &#x53BB;&#x63CF;&#x8FF0;&#x539F;&#x672C;&#x7684; DOM &#x7ED3;&#x6784;&#x3002;
&#x5728;&#x9700;&#x8981;&#x66F4;&#x65B0; DOM &#x65F6;&#x4E0D;&#x76F4;&#x63A5;&#x64CD;&#x4F5C; DOM &#x6811;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;&#x66F4;&#x65B0; JavaScript Object &#x540E;&#x518D;&#x6620;&#x5C04;&#x6210; DOM &#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x865A;&#x62DF; DOM &#x7684;&#x4F18;&#x70B9;&#x5728;&#x4E8E;&#xFF1A;</p>
<ul>
<li>&#x56E0;&#x4E3A;&#x64CD;&#x4F5C; DOM &#x6811;&#x662F;&#x9AD8;&#x8017;&#x65F6;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5C3D;&#x91CF;&#x51CF;&#x5C11; DOM &#x6811;&#x64CD;&#x4F5C;&#x80FD;&#x4F18;&#x5316;&#x7F51;&#x9875;&#x6027;&#x80FD;&#x3002;&#x800C; DOM Diff &#x7B97;&#x6CD5;&#x80FD;&#x627E;&#x51FA;2&#x4E2A;&#x4E0D;&#x540C; Object &#x7684;&#x6700;&#x5C0F;&#x5DEE;&#x5F02;&#xFF0C;&#x5F97;&#x51FA;&#x6700;&#x5C0F; DOM &#x64CD;&#x4F5C;&#xFF1B;</li>
<li>&#x865A;&#x62DF; DOM &#x7684;&#x5728;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#x4E0D;&#x4EC5;&#x4EC5;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x64CD;&#x4F5C; DOM &#x6811;&#x6765;&#x8868;&#x793A;&#x51FA;&#x7ED3;&#x679C;&#xFF0C;&#x4E5F;&#x80FD;&#x6709;&#x5176;&#x5B83;&#x7684;&#x8868;&#x793A;&#x65B9;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#x628A;&#x865A;&#x62DF; DOM &#x6E32;&#x67D3;&#x6210;&#x5B57;&#x7B26;&#x4E32;(&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6E32;&#x67D3;)&#xFF0C;&#x6216;&#x8005;&#x6E32;&#x67D3;&#x6210;&#x624B;&#x673A; App &#x539F;&#x751F;&#x7684; UI &#x7EC4;&#x4EF6;( React Native)&#x3002;</li>
</ul>
<p>&#x4EE5; React &#x4E3A;&#x4F8B;&#xFF0C;&#x6838;&#x5FC3;&#x6A21;&#x5757; <code>react</code> &#x8D1F;&#x8D23;&#x7BA1;&#x7406; React &#x7EC4;&#x4EF6;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#xFF0C;&#x800C;&#x5177;&#x4F53;&#x7684;&#x6E32;&#x67D3;&#x5DE5;&#x4F5C;&#x53EF;&#x4EE5;&#x4EA4;&#x7ED9; <code>react-dom</code> &#x6A21;&#x5757;&#x6765;&#x8D1F;&#x8D23;&#x3002;</p>
<p><code>react-dom</code> &#x5728;&#x6E32;&#x67D3;&#x865A;&#x62DF; DOM &#x6811;&#x65F6;&#x6709;2&#x4E2D;&#x65B9;&#x5F0F;&#x53EF;&#x9009;&#xFF1A;</p>
<ul>
<li>&#x901A;&#x8FC7; <code>render()</code> &#x51FD;&#x6570;&#x53BB;&#x64CD;&#x4F5C;&#x6D4F;&#x89C8;&#x5668; DOM &#x6811;&#x6765;&#x5C55;&#x793A;&#x51FA;&#x7ED3;&#x679C;&#x3002;</li>
<li>&#x901A;&#x8FC7; <code>renderToString()</code> &#x8BA1;&#x7B97;&#x51FA;&#x8868;&#x793A;&#x865A;&#x62DF; DOM &#x7684; HTML &#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
</ul>
<p>&#x6784;&#x5EFA;&#x540C;&#x6784;&#x5E94;&#x7528;&#x7684;&#x6700;&#x7EC8;&#x76EE;&#x7684;&#x662F;&#x4ECE;&#x4E00;&#x4EFD;&#x9879;&#x76EE;&#x6E90;&#x7801;&#x4E2D;&#x6784;&#x5EFA;&#x51FA;2&#x4EFD; JavaScript &#x4EE3;&#x7801;&#xFF0C;&#x4E00;&#x4EFD;&#x7528;&#x4E8E;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x8FD0;&#x884C;&#xFF0C;&#x4E00;&#x4EFD;&#x7528;&#x4E8E;&#x5728; Node.js &#x73AF;&#x5883;&#x4E2D;&#x8FD0;&#x884C;&#x6E32;&#x67D3;&#x51FA; HTML&#x3002;
&#x5176;&#x4E2D;&#x7528;&#x4E8E;&#x5728; Node.js &#x73AF;&#x5883;&#x4E2D;&#x8FD0;&#x884C;&#x7684; JavaScript &#x4EE3;&#x7801;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4EE5;&#x4E0B;&#x51E0;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x4E0D;&#x80FD;&#x5305;&#x542B;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x63D0;&#x4F9B;&#x7684; API&#xFF0C;&#x4F8B;&#x5982;&#x4F7F;&#x7528; <code>document</code> &#x8FDB;&#x884C; DOM &#x64CD;&#x4F5C;&#xFF0C; &#x3000;&#x56E0;&#x4E3A; Node.js &#x4E0D;&#x652F;&#x6301;&#x8FD9;&#x4E9B; API&#xFF1B;</li>
<li>&#x4E0D;&#x80FD;&#x5305;&#x542B; CSS &#x4EE3;&#x7801;&#xFF0C;&#x56E0;&#x4E3A;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x76EE;&#x7684;&#x662F;&#x6E32;&#x67D3;&#x51FA; HTML &#x5185;&#x5BB9;&#xFF0C;&#x6E32;&#x67D3;&#x51FA; CSS &#x4EE3;&#x7801;&#x4F1A;&#x589E;&#x52A0;&#x989D;&#x5916;&#x7684;&#x8BA1;&#x7B97;&#x91CF;&#xFF0C;&#x5F71;&#x54CD;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x6027;&#x80FD;&#xFF1B;</li>
<li>&#x4E0D;&#x80FD;&#x50CF;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x7684;&#x8F93;&#x51FA;&#x4EE3;&#x7801;&#x90A3;&#x6837;&#x628A; <code>node_modules</code> &#x91CC;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x6A21;&#x5757;&#x548C; Node.js &#x539F;&#x751F;&#x6A21;&#x5757;(&#x4F8B;&#x5982; <code>fs</code> &#x6A21;&#x5757;)&#x6253;&#x5305;&#x8FDB;&#x53BB;&#xFF0C;&#x800C;&#x662F;&#x9700;&#x8981;&#x901A;&#x8FC7; CommonJS &#x89C4;&#x8303;&#x53BB;&#x5F15;&#x5165;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x3002;</li>
<li>&#x9700;&#x8981;&#x901A;&#x8FC7; CommonJS &#x89C4;&#x8303;&#x5BFC;&#x51FA;&#x4E00;&#x4E2A;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x7528;&#x4E8E;&#x5728; HTTP &#x670D;&#x52A1;&#x5668;&#x4E2D;&#x53BB;&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x6E32;&#x67D3;&#x51FA; HTML &#x5185;&#x5BB9;&#x8FD4;&#x56DE;&#x3002;</li>
</ul>
<h2 id="&#x89E3;&#x51B3;&#x65B9;&#x6848;">&#x89E3;&#x51B3;&#x65B9;&#x6848;</h2>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6539;&#x9020;&#x5728;<a href="3-6&#x4F7F;&#x7528;React&#x6846;&#x67B6;.html">3-6&#x4F7F;&#x7528; React &#x6846;&#x67B6;</a>&#x4E2D;&#x4ECB;&#x7ECD;&#x7684; React &#x9879;&#x76EE;&#xFF0C;&#x4E3A;&#x5B83;&#x589E;&#x52A0;&#x6784;&#x5EFA;&#x540C;&#x6784;&#x5E94;&#x7528;&#x7684;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x8981;&#x4ECE;&#x4E00;&#x4EFD;&#x6E90;&#x7801;&#x6784;&#x5EFA;&#x51FA;2&#x4EFD;&#x4E0D;&#x540C;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x9700;&#x8981;&#x6709;2&#x4EFD; Webpack &#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5206;&#x522B;&#x4E0E;&#x4E4B;&#x5BF9;&#x5E94;&#x3002;
&#x6784;&#x5EFA;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x7684;&#x914D;&#x7F6E;&#x548C;&#x524D;&#x9762;&#x8BB2;&#x7684;&#x6CA1;&#x6709;&#x5DEE;&#x522B;&#xFF0C;&#x672C;&#x8282;&#x4FA7;&#x91CD;&#x4E8E;&#x8BB2;&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x7528;&#x4E8E;&#x6784;&#x5EFA;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x4EE3;&#x7801;&#x7684; <code>webpack.config.js</code> &#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4FDD;&#x7559;&#x4E0D;&#x53D8;&#xFF0C;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x4E13;&#x95E8;&#x7528;&#x4E8E;&#x6784;&#x5EFA;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x4EE3;&#x7801;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6; <code>webpack_server.config.js</code>&#xFF0C;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> nodeExternals = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack-node-externals&apos;</span>);

<span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-comment">// JS &#x6267;&#x884C;&#x5165;&#x53E3;&#x6587;&#x4EF6;</span>
  entry: <span class="hljs-string">&apos;./main_server.js&apos;</span>,
  <span class="hljs-comment">// &#x4E3A;&#x4E86;&#x4E0D;&#x628A; Node.js &#x5185;&#x7F6E;&#x7684;&#x6A21;&#x5757;&#x6253;&#x5305;&#x8FDB;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982; fs net &#x6A21;&#x5757;&#x7B49;</span>
  target: <span class="hljs-string">&apos;node&apos;</span>,
  <span class="hljs-comment">// &#x4E3A;&#x4E86;&#x4E0D;&#x628A; node_modules &#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x6A21;&#x5757;&#x6253;&#x5305;&#x8FDB;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x4E2D;</span>
  externals: [nodeExternals()],
  output: {
    <span class="hljs-comment">// &#x4E3A;&#x4E86;&#x4EE5; CommonJS2 &#x89C4;&#x8303;&#x5BFC;&#x51FA;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x7ED9;&#x91C7;&#x7528; Node.js &#x7F16;&#x5199;&#x7684; HTTP &#x670D;&#x52A1;&#x8C03;&#x7528;</span>
    libraryTarget: <span class="hljs-string">&apos;commonjs2&apos;</span>,
    <span class="hljs-comment">// &#x628A;&#x6700;&#x7EC8;&#x53EF;&#x5728; Node.js &#x4E2D;&#x8FD0;&#x884C;&#x7684;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x5230;&#x4E00;&#x4E2A; bundle_server.js &#x6587;&#x4EF6;</span>
    filename: <span class="hljs-string">&apos;bundle_server.js&apos;</span>,
    <span class="hljs-comment">// &#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x90FD;&#x653E;&#x5230; dist &#x76EE;&#x5F55;&#x4E0B;</span>
    path: path.resolve(__dirname, <span class="hljs-string">&apos;./dist&apos;</span>),
  },
  <span class="hljs-built_in">module</span>: {
    rules: [
      {
        test: <span class="hljs-regexp">/\.js$/</span>,
        use: [<span class="hljs-string">&apos;babel-loader&apos;</span>],
        exclude: path.resolve(__dirname, <span class="hljs-string">&apos;node_modules&apos;</span>),
      },
      {
        <span class="hljs-comment">// CSS &#x4EE3;&#x7801;&#x4E0D;&#x80FD;&#x88AB;&#x6253;&#x5305;&#x8FDB;&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x53BB;&#xFF0C;&#x5FFD;&#x7565;&#x6389; CSS &#x6587;&#x4EF6;</span>
        test: <span class="hljs-regexp">/\.css$/</span>,
        use: [<span class="hljs-string">&apos;ignore-loader&apos;</span>],
      },
    ]
  },
  devtool: <span class="hljs-string">&apos;source-map&apos;</span> <span class="hljs-comment">// &#x8F93;&#x51FA; source-map &#x65B9;&#x4FBF;&#x76F4;&#x63A5;&#x8C03;&#x8BD5; ES6 &#x6E90;&#x7801;</span>
};
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6709;&#x51E0;&#x4E2A;&#x5173;&#x952E;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5206;&#x522B;&#x662F;&#xFF1A;</p>
<ul>
<li><code>target: &apos;node&apos;</code> &#x7531;&#x4E8E;&#x8F93;&#x51FA;&#x4EE3;&#x7801;&#x7684;&#x8FD0;&#x884C;&#x73AF;&#x5883;&#x662F; Node.js&#xFF0C;&#x6E90;&#x7801;&#x4E2D;&#x4F9D;&#x8D56;&#x7684; Node.js &#x539F;&#x751F;&#x6A21;&#x5757;&#x6CA1;&#x5FC5;&#x8981;&#x6253;&#x5305;&#x8FDB;&#x53BB;&#xFF1B;</li>
<li><code>externals: [nodeExternals()]</code> <a href="https://github.com/liady/webpack-node-externals" target="_blank">webpack-node-externals</a> &#x7684;&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x9632;&#x6B62; node_modules &#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x6A21;&#x5757;&#x88AB;&#x6253;&#x5305;&#x8FDB;&#x53BB;&#xFF0C;&#x56E0;&#x4E3A; Node.js &#x9ED8;&#x8BA4;&#x4F1A;&#x53BB; node_modules &#x76EE;&#x5F55;&#x4E0B;&#x5BFB;&#x627E;&#x548C;&#x4F7F;&#x7528;&#x7B2C;&#x4E09;&#x65B9;&#x6A21;&#x5757;&#xFF1B;</li>
<li><code>{test: /\.css$/, use: [&apos;ignore-loader&apos;]}</code> &#x5FFD;&#x7565;&#x6389;&#x4F9D;&#x8D56;&#x7684; CSS &#x6587;&#x4EF6;&#xFF0C;CSS &#x4F1A;&#x5F71;&#x54CD;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x6027;&#x80FD;&#xFF0C;&#x53C8;&#x662F;&#x505A;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x4E0D;&#x91CD;&#x8981;&#x7684;&#x90E8;&#x5206;&#xFF1B;</li>
<li><code>libraryTarget: &apos;commonjs2&apos;</code> &#x4EE5; CommonJS2 &#x89C4;&#x8303;&#x5BFC;&#x51FA;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x4F9B;&#x7ED9;&#x91C7;&#x7528; Node.js &#x7F16;&#x5199;&#x7684; HTTP &#x670D;&#x52A1;&#x5668;&#x4EE3;&#x7801;&#x8C03;&#x7528;&#x3002;</li>
</ul>
<p>&#x4E3A;&#x4E86;&#x6700;&#x5927;&#x9650;&#x5EA6;&#x7684;&#x590D;&#x7528;&#x4EE3;&#x7801;&#xFF0C;&#x9700;&#x8981;&#x8C03;&#x6574;&#x4E0B;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#xFF1A;</p>
<p>&#x628A;&#x9875;&#x9762;&#x7684;&#x6839;&#x7EC4;&#x4EF6;&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;&#x6587;&#x4EF6; <code>AppComponent.js</code>&#xFF0C;&#x8BE5;&#x6587;&#x4EF6;&#x53EA;&#x80FD;&#x5305;&#x542B;&#x6839;&#x7EC4;&#x4EF6;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4E0D;&#x80FD;&#x5305;&#x542B;&#x6E32;&#x67D3;&#x5165;&#x53E3;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x800C;&#x4E14;&#x9700;&#x8981;&#x5BFC;&#x51FA;&#x6839;&#x7EC4;&#x4EF6;&#x4EE5;&#x4F9B;&#x7ED9;&#x6E32;&#x67D3;&#x5165;&#x53E3;&#x8C03;&#x7528;&#xFF0C;<code>AppComponent.js</code> &#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;./main.css&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello,Webpack<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
  }
}
</code></pre>
<p>&#x5206;&#x522B;&#x4E3A;&#x4E0D;&#x540C;&#x73AF;&#x5883;&#x7684;&#x6E32;&#x67D3;&#x5165;&#x53E3;&#x5199;&#x4E24;&#x4EFD;&#x4E0D;&#x540C;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5206;&#x522B;&#x662F;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6E32;&#x67D3; DOM &#x7684; <code>main_browser.js</code> &#x6587;&#x4EF6;&#xFF0C;&#x548C;&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3; HTML &#x5B57;&#x7B26;&#x4E32;&#x7684; <code>main_server.js</code> &#x6587;&#x4EF6;&#x3002;</p>
<p><code>main_browser.js</code> &#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { render } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> { AppComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./AppComponent&apos;</span>;

<span class="hljs-comment">// &#x628A;&#x6839;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x5230; DOM &#x6811;&#x4E0A;</span>
render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppComponent</span>/&gt;</span></span>, <span class="hljs-built_in">window</span>.document.getElementById(<span class="hljs-string">&apos;app&apos;</span>));
</code></pre>
<p><code>main_server.js</code> &#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { renderToString } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom/server&apos;</span>;
<span class="hljs-keyword">import</span> { AppComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./AppComponent&apos;</span>;

<span class="hljs-comment">// &#x5BFC;&#x51FA;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x4EE5;&#x7ED9;&#x91C7;&#x7528; Node.js &#x7F16;&#x5199;&#x7684; HTTP &#x670D;&#x52A1;&#x5668;&#x4EE3;&#x7801;&#x8C03;&#x7528;</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// &#x628A;&#x6839;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210; HTML &#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">return</span> renderToString(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppComponent</span>/&gt;</span></span>)
}
</code></pre>
<p>&#x4E3A;&#x4E86;&#x80FD;&#x628A;&#x6E32;&#x67D3;&#x7684;&#x5B8C;&#x6574; HTML &#x6587;&#x4EF6;&#x901A;&#x8FC7; HTTP &#x670D;&#x52A1;&#x8FD4;&#x56DE;&#x7ED9;&#x8BF7;&#x6C42;&#x7AEF;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x7528; Node.js &#x7F16;&#x5199;&#x4E00;&#x4E2A; HTTP &#x670D;&#x52A1;&#x5668;&#x3002;
&#x7531;&#x4E8E;&#x672C;&#x8282;&#x4E0D;&#x4E13;&#x6CE8;&#x4E8E;&#x5C06; HTTP &#x670D;&#x52A1;&#x5668;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x91C7;&#x7528;&#x4E86; ExpressJS &#x6765;&#x5B9E;&#x73B0;&#xFF0C;<code>http_server.js</code> &#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> { render } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./dist/bundle_server&apos;</span>);
<span class="hljs-keyword">const</span> app = express();

<span class="hljs-comment">// &#x8C03;&#x7528;&#x6784;&#x5EFA;&#x51FA;&#x7684; bundle_server.js &#x4E2D;&#x66B4;&#x9732;&#x51FA;&#x7684;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x518D;&#x62FC;&#x63A5;&#x4E0B; HTML &#x6A21;&#x7248;&#xFF0C;&#x5F62;&#x6210;&#x5B8C;&#x6574;&#x7684; HTML &#x6587;&#x4EF6;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
  res.send(<span class="hljs-string">`
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;app&quot;&gt;<span class="hljs-subst">${render()}</span>&lt;/div&gt;
&lt;!--&#x5BFC;&#x5165; Webpack &#x8F93;&#x51FA;&#x7684;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x6E32;&#x67D3;&#x7684; JS &#x6587;&#x4EF6;--&gt;
&lt;script src=&quot;./dist/bundle_browser.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
  `</span>);
});

<span class="hljs-comment">// &#x5176;&#x5B83;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x8FD4;&#x56DE;&#x5BF9;&#x5E94;&#x7684;&#x672C;&#x5730;&#x6587;&#x4EF6;</span>
app.use(express.static(<span class="hljs-string">&apos;.&apos;</span>));

app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;app listening on port 3000!&apos;</span>)
});
</code></pre>
<p>&#x518D;&#x5B89;&#x88C5;&#x65B0;&#x5F15;&#x5165;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x4F9D;&#x8D56;&#xFF1A;</p>
<pre><code class="lang-bash"><span class="hljs-comment"># &#x5B89;&#x88C5; Webpack &#x6784;&#x5EFA;&#x4F9D;&#x8D56;</span>
npm i -D css-loader style-loader ignore-loader webpack-node-externals
<span class="hljs-comment"># &#x5B89;&#x88C5; HTTP &#x670D;&#x52A1;&#x5668;&#x4F9D;&#x8D56;</span>
npm i -S express
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x6240;&#x6709;&#x51C6;&#x5907;&#x5DE5;&#x4F5C;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6267;&#x884C;&#x6784;&#x5EFA;&#xFF0C;&#x7F16;&#x8BD1;&#x51FA;&#x76EE;&#x6807;&#x6587;&#x4EF6;&#xFF1A;</p>
<ul>
<li>&#x6267;&#x884C;&#x547D;&#x4EE4; <code>webpack --config webpack_server.config.js</code> &#x6784;&#x5EFA;&#x51FA;&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684; <code>./dist/bundle_server.js</code> &#x6587;&#x4EF6;&#x3002;</li>
<li>&#x6267;&#x884C;&#x547D;&#x4EE4; <code>webpack</code> &#x6784;&#x5EFA;&#x51FA;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x8FD0;&#x884C;&#x7684; <code>./dist/bundle_browser.js</code> &#x6587;&#x4EF6;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E3A; <code>webpack.config.js</code>&#x3002;</li>
</ul>
<p>&#x6784;&#x5EFA;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x6267;&#x884C; <code>node ./http_server.js</code> &#x542F;&#x52A8; HTTP &#x670D;&#x52A1;&#x5668;&#x540E;&#xFF0C;&#x518D;&#x7528;&#x6D4F;&#x89C8;&#x5668;&#x53BB;&#x8BBF;&#x95EE; <code>http://localhost:3000</code> &#x5C31;&#x80FD;&#x770B;&#x5230; <code>Hello,Webpack</code> &#x4E86;&#x3002;
&#x4F46;&#x662F;&#x4E3A;&#x4E86;&#x9A8C;&#x8BC1;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x6253;&#x5F00;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x5F00;&#x53D1;&#x5DE5;&#x5177;&#x4E2D;&#x7684;&#x7F51;&#x7EDC;&#x6293;&#x5305;&#x4E00;&#x680F;&#xFF0C;&#x518D;&#x91CD;&#x65B0;&#x5237;&#x65B0;&#x6D4F;&#x89C8;&#x5668;&#x540E;&#xFF0C;&#x5C31;&#x80FD;&#x6293;&#x5230;&#x8BF7;&#x6C42; HTML &#x7684;&#x5305;&#x4E86;&#xFF0C;&#x6293;&#x5305;&#x6548;&#x679C;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="img/3-11server-render.png" alt="&#x56FE;3.9.1 &#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x6293;&#x5305;"></p>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x6E32;&#x67D3;&#x51FA;&#x5185;&#x5BB9;&#x540E;&#x7684; HTML &#x800C;&#x4E0D;&#x662F; HTML &#x6A21;&#x7248;&#xFF0C;&#x8FD9;&#x8BF4;&#x660E;&#x540C;&#x6784;&#x5E94;&#x7528;&#x7684;&#x6539;&#x9020;&#x5B8C;&#x6210;&#x3002;</p>
<blockquote>
<p>&#x672C;&#x5B9E;&#x4F8B;<a href="http://webpack.wuhaolin.cn/3-11&#x6784;&#x5EFA;&#x540C;&#x6784;&#x5E94;&#x7528;.zip" target="_blank">&#x63D0;&#x4F9B;&#x9879;&#x76EE;&#x5B8C;&#x6574;&#x4EE3;&#x7801;</a></p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3-10管理多个单页应用.html" class="navigation navigation-prev " aria-label="Previous page: 3-10 管理多个单页应用">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3-12构建Electron应用.html" class="navigation navigation-next " aria-label="Next page: 3-12 构建 Electron 应用">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3-11 构建同构应用","level":"1.5.11","depth":2,"next":{"title":"3-12 构建 Electron 应用","level":"1.5.12","depth":2,"path":"3实战/3-12构建Electron应用.md","ref":"3实战/3-12构建Electron应用.md","articles":[]},"previous":{"title":"3-10 管理多个单页应用","level":"1.5.10","depth":2,"path":"3实战/3-10管理多个单页应用.md","ref":"3实战/3-10管理多个单页应用.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github-buttons","ga","modal","adsense"],"root":"./docs","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"search":{},"adsense":{"client":"ca-pub-9697944574373240","slot":"9319793387","format":"auto","element":".page-inner section","position":"top"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"modal":{"closable":true,"excludeUrls":["\\?q=.+$","index.html","前言.html","\\/$","1-.+","2-2Output","3-1使用ES6语言","3-2使用TypeScript语言","3-4使用SCSS语言.html","3-5使用PostCSS.html","3-6使用React框架","3-7使用Vue框架","3-8使用Angular2框架","3-9为单页应用生成HTML","3-10管理多个单页应用","3-17通过Node.jsAPI启动Webpack","3-20加载SVG"],"html":"<div style='text-align: center'><a href='http://union-click.jd.com/jdc?d=1AMcaz'><img src='http://p0.meituan.net/scarlett/df16c51ffb95186df6f75d8c0e22b965842464.png' width='306px' height='411px'><a/> <h2><a target='_blank' href='http://union-click.jd.com/jdc?d=1AMcaz'>购买本书纸质版</a></h2></div>"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"gwuhaolin","type":"follow","size":"small","width":"150"},{"user":"gwuhaolin","repo":"dive-into-webpack","type":"star","size":"small"}]},"ga":{"configuration":"auto","token":"UA-54964850-3"},"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"吴浩麟","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"9787121331725","variables":{},"title":"深入浅出 Webpack","language":"zh-hans","gitbook":"*","description":"本书按照入门、配置、实战、优化、原理的路线层层深入，涵盖了 Webpack 的方方面面。对于小白，本书可以作为 Webpack 入门教程，对于老手，本书可以进一步加深你对 Webpack 的理解。"},"file":{"path":"3实战/3-11构建同构应用.md","mtime":"2019-02-28T08:16:23.290Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-12-22T08:51:47.376Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-ga/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-modal/index.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-adsense/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

